package com.yssoft.service.supervise;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.yssoft.entity.core.TEnterprise;
import com.yssoft.entity.core.TProductClassify;
import com.yssoft.entity.supplier.BillCount;
import com.yssoft.entity.supplier.BillCountCustom;
import com.yssoft.entity.supplier.TEnterpriseCustom;
import com.yssoft.mapper.core.TEnterpriseMapper;
import com.yssoft.mapper.supplier.TBillCountMapper;
import com.yssoft.utils.LayuiUtil;
import com.yssoft.utils.UserHelper;
import com.yssoft.utils.verificationCode.DateUtil;

@Service
public class BillCountService {
 
	@Resource
	private TBillCountMapper billCountMapper;
	public JSONObject queryBillPage(int page, int limit, BillCountCustom custom) throws IllegalAccessException, ClassNotFoundException 	{
		//当参数为空处理，防止空指针报错
        if(custom==null ) {
        	custom=new BillCountCustom();
        }
        //必须在分页之前查询此用户数据，否则会被强制分页
		List<Map<String, Object>>  customList=null;
		if(StringUtils.isNoneBlank(custom.getAreaId()) ) {
			if(custom.getAreaId().equals("100000")){
				custom.setIsNationwide("y");
			}else{
				customList= billCountMapper.getCustomerIdByAreaId(custom.getAreaId());
				if(customList == null || customList.size() == 0){
	    			List<BillCountCustom> newlist = Lists.newArrayList();
	    			JSONObject jsonObject =LayuiUtil.getJSONObjectByObject(newlist,0,"","");
	    	        return jsonObject;
	    		}
			}
        }
        PageHelper.startPage(page,limit);//下面第一个查询结果进行分页
        custom.setDeleteFlag(0);
        List<TEnterprise> eList=null;
       
        //选择企业参数
        if(StringUtils.isNoneBlank(custom.getJsonParam())) {
        	String json=custom.getJsonParam();
            eList=JSONArray.parseArray(json, TEnterprise.class);
        }
        //开始时间和结束时间
        if(custom.getWorksheetTime()!=null && custom.getWorksheetTime().indexOf(" - ")>0){
        	String[] time= custom.getWorksheetTime().split(" - ");
        	custom.setBeginTime(  DateUtil.strToDate(time[0].trim()) );
        	custom.setEndTime(  DateUtil.strToDate(time[1].trim()) );
        } 
        
        //查询分页数据
       
        List<BillCountCustom> list =billCountMapper.selectBillStatisticsPage(custom,null,eList,customList);
        
        //查询总条数
        int count = billCountMapper.selectBillStatisticsPageCount(custom,null,eList,customList);
        JSONObject jsonObject =LayuiUtil.getJSONObjectByObject(list,count,"","");
        return jsonObject;
	}
	public JSONObject queryEnterprisePage(int page, int limit, String enterpriseName) throws 			IllegalAccessException, ClassNotFoundException {		 
	        //PageHelper.startPage(page,limit);//下面第一个查询结果进行分页
	        Map<String,Object> param=new HashMap<>();
	        if(StringUtils.isNoneBlank(enterpriseName)) {
	        	param.put("name",enterpriseName);
	        }
	        
	        List<TEnterpriseCustom> enterpriseList = billCountMapper.queryEnterprisePage(param);
	        int count = billCountMapper.queryEnterprisePageCount(param);//查询条数
	        JSONObject jsonObject =LayuiUtil.getJSONObjectByObject(enterpriseList,count,"","");
	        return jsonObject;
	}
	public List<TProductClassify> queryProductClassifyList(TProductClassify classify) {
		return billCountMapper.queryProductClassifyList(classify);
	}
	
	/**
	 * @方法名称: queryProductClassifyList
	 * @功能描述: 根据销货单明细id获取数据
	 * @作者: 洪丰
	 * @创建时间:2018年7月9日 下午3:41:14
	 * @param id
	 * @return BillCountCustom
	 */
	public BillCountCustom querybillOne(Integer id) {
		return billCountMapper.selectBillStatisticsOne(id);
	}
}
